﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Messaging;
using System.Reflection;
using Pixysoft.Framework.Configurations;
using Pixysoft.Framework.Remoting.Controller;
using Pixysoft.Net.Crawler;
using Pixysoft.Framework.Verifications;

namespace Pixysoft.Framework.Remoting.Core
{
    class CacheClearRemoteChannel<T> : RealProxy, IRemoteChannel<T>
    {
        Type type = null;

        public CacheClearRemoteChannel()
            : base(typeof(T))
        {
            this.type = typeof(T);
        }

        public string Login(string username, string password)
        {
            //do nothing

            return null;
        }

        public bool IsLogin()
        {
            return false;
        }

        public void Logout()
        {
        }

        public void Dispose()
        {
        }

        public T RemoteProxy
        {
            get { return (T)this.GetTransparentProxy(); }
        }

        public override IMessage Invoke(IMessage msg)
        {
            IMethodCallMessage methodCall = msg as IMethodCallMessage;

            MethodInfo method = methodCall.MethodBase as MethodInfo;

            if (method == null)
                return new ReturnMessage(Exceptions.UnexpectedResultException("only support method."), methodCall);

            if (method == typeof(object).GetMethod("GetType"))
            {
                return new ReturnMessage(type, null, 0, null, methodCall);
            }
            else if (method == typeof(object).GetMethod("Equals", new Type[] { typeof(object) }))
            {
                return new ReturnMessage(false, null, 0, null, methodCall);
            }
            else if (method == typeof(object).GetMethod("GetHashCode"))
            {
                return new ReturnMessage(this.GetHashCode(), null, 0, null, methodCall);
            }
            else if (method == typeof(object).GetMethod("ToString"))
            {
                return new ReturnMessage(this.ToString(), null, 0, null, methodCall);
            }

            //调用序列化

            IXmlNode node = null;

            try
            {
                node = ReflectionController.Instance.GetMethodDeclaration(method, methodCall.Args);
            }
            catch (Exception ex)
            {
                return new ReturnMessage(ex, methodCall);
            }

            string rawrequest = node.Serialize();

            CacheController.Instance.Remove(rawrequest);


            //返回远程结果

            if (method.ReturnType.Equals(typeof(void)))
                return new ReturnMessage(null, null, 0, null, methodCall);

            object returnvalue = null;

            try
            {
                returnvalue = ReflectionController.Instance.GetDeserializeRemoteCallResult(method.ReturnType, null);
            }
            catch (Exception ex)
            {
                return new ReturnMessage(ex, methodCall);
            }

            return new ReturnMessage(returnvalue, null, 0, null, methodCall);
        }

    }
}
